package com.ruoyi.monitor.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.monitor.Vo.MsgCountVo;
import com.ruoyi.monitor.domain.SysMsg;
import com.ruoyi.monitor.utils.Common.Constant;
import com.ruoyi.monitor.utils.Common.DateUtils;
import com.ruoyi.monitor.utils.Email.Mail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.monitor.mapper.SysMsgMapper;
import com.ruoyi.monitor.service.ISysMsgService;

/**
 * 邮箱发送Service业务层处理
 * 
 * @author xzx
 * @date 2023-02-22
 */
@Service
public class SysMsgServiceImpl extends ServiceImpl<SysMsgMapper, SysMsg> implements ISysMsgService
{
    @Autowired
    private SysMsgMapper sysMsgMapper;

    @Override
    public List<MsgCountVo> countAlarm(String type) {
        DateTime beginOfMonth = DateUtil.beginOfMonth(new Date());
        Date pastDate = DateUtils.getPastDate(5, beginOfMonth);
        if ("1".equals(type)){
            //所有
            QueryWrapper<SysMsg> wrapper = new QueryWrapper<>();
            wrapper.apply("rank like '%"+ Constant.areaCode + "%'");
            List<MsgCountVo> msgCountVos = sysMsgMapper.countsOfAlarm(wrapper);
            return msgCountVos;
        }else if ("2".equals(type)){
            //本月
            List<MsgCountVo> msgCountThisMonth = sysMsgMapper.countsOfAlarm(new QueryWrapper<SysMsg>()
                    .apply("rank like '%"+ Constant.areaCode + "%'")
                    .apply("date >=" +"'"+ DateUtils.format(DateUtil.endOfMonth(pastDate))+"'"
                    ).apply("date<=" +"'"+ DateUtils.format(DateUtil.endOfMonth(new Date()))+"'"));
            System.out.println(msgCountThisMonth.size());
            return msgCountThisMonth;
        }else if ("3".equals(type)){
            //上月
            System.out.println("-------------上月数据-------------");
            List<MsgCountVo> MsgCountLastMonth = sysMsgMapper.countsOfAlarm(new QueryWrapper<SysMsg>()
                    .apply("rank like '%"+ Constant.areaCode + "%'")
                    .apply("date >=" +"'"+DateUtils.format(DateUtil.beginOfMonth(pastDate))+"'"
                            + " and date<=" +"'"+ DateUtils.format(DateUtil.endOfMonth(pastDate))+"'"
                    ));
            System.out.println(MsgCountLastMonth.size());
            System.out.println("-------------上月数据-------------");
            return MsgCountLastMonth;
        }
        return null;
    }

    @Override
    public synchronized List<MsgCountVo> countMsgs(String type) {
        DateTime beginOfMonth = DateUtil.beginOfMonth(new Date());
        Date pastDate = DateUtils.getPastDate(5, beginOfMonth);
        if ("1".equals(type)) {
            System.out.println("-------------本月数据-------------");
            List<MsgCountVo> MsgCountThisMonth = sysMsgMapper.countsOfDays(new QueryWrapper<SysMsg>()
                    .apply("date >=" +"'"+ DateUtils.format(DateUtil.endOfMonth(pastDate))+"'"
                    ).apply("date<=" +"'"+ DateUtils.format(DateUtil.endOfMonth(new Date()))+"'"));
            System.out.println(MsgCountThisMonth.size());
            System.out.println("-------------本月数据-------------");
            return MsgCountThisMonth;
        }else if ("2".equals(type)){

            System.out.println("-------------上月数据-------------");
            List<MsgCountVo> MsgCountLastMonth = sysMsgMapper.countsOfDays(new QueryWrapper<SysMsg>()
                    .apply("date >=" +"'"+DateUtils.format(DateUtil.beginOfMonth(pastDate))+"'"
                            + " and date<=" +"'"+ DateUtils.format(DateUtil.endOfMonth(pastDate))+"'"
                            ));
            System.out.println(MsgCountLastMonth.size());
            System.out.println("-------------上月数据-------------");
            return MsgCountLastMonth;
        }

        return null;
    }

    @Override
    public List<MsgCountVo> countMsgsFromTo(Date from, Date to) {
        List<MsgCountVo> MsgCount = sysMsgMapper.countsOfDays(new QueryWrapper<SysMsg>()
                .apply("date >=" +"'"+ DateUtils.format(from)+"'"
                        + " and date<=" +"'"+ DateUtils.format(DateUtil.endOfMonth(to))+"'"));
        return MsgCount;
    }

    /**
     * 查询邮箱发送
     * 
     * @param id 邮箱发送主键
     * @return 邮箱发送
     */
    @Override
    public SysMsg selectSysMsgById(Integer id)
    {
        return sysMsgMapper.selectSysMsgById(id);
    }

    /**
     * 查询邮箱发送列表
     * 
     * @param sysMsg 邮箱发送
     * @return 邮箱发送
     */
    @Override
    public List<SysMsg> selectSysMsgList(SysMsg sysMsg)
    {
        return sysMsgMapper.selectSysMsgList(sysMsg);
    }

    /**
     * 新增邮箱发送
     * 
     * @param sysMsg 邮箱发送
     * @return 结果
     */
    @Override
    public int insertSysMsg(SysMsg sysMsg)
    {
        return sysMsgMapper.insertSysMsg(sysMsg);
    }

    /**
     * 修改邮箱发送
     * 
     * @param sysMsg 邮箱发送
     * @return 结果
     */
    @Override
    public int updateSysMsg(SysMsg sysMsg)
    {
        return sysMsgMapper.updateSysMsg(sysMsg);
    }

    /**
     * 批量删除邮箱发送
     * 
     * @param ids 需要删除的邮箱发送主键
     * @return 结果
     */
    @Override
    public int deleteSysMsgByIds(Integer[] ids)
    {
        return sysMsgMapper.deleteSysMsgByIds(ids);
    }

    /**
     * 删除邮箱发送信息
     * 
     * @param id 邮箱发送主键
     * @return 结果
     */
    @Override
    public int deleteSysMsgById(Integer id)
    {
        return sysMsgMapper.deleteSysMsgById(id);
    }
}
